package com.rzico.sbl.ui.manager

import androidx.core.view.isVisible
import com.jeremyliao.liveeventbus.LiveEventBus
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityCommonListBinding
import com.rzico.sbl.model.BaseUrl
import com.rzico.sbl.model.MasterTransferData
import com.rzico.sbl.viewmodel.MasterViewModel
import com.xinnuo.common.extend.*
import com.xinnuo.common.helper.ActivityStack
import com.xinnuo.common.helper.getTimeString
import com.xinnuo.common.decoration.SpaceTBDecoration
import com.xinnuo.common_ui.base.BaseActivity
import com.xinnuo.common_ui.base.addItems
import com.xinnuo.common_ui.base.loadLinear
import com.xinnuo.common_ui.utils.showHintDialog
import com.xinnuo.slimadapter.SlimAdapter

class MasterTransferRecordActivity : BaseActivity(R.layout.activity_common_list) {

    private val mBinding by lazy { ActivityCommonListBinding.bind(getRootView()) }

    override fun getViewModel() = get<MasterViewModel>()

    override fun initData() {
        initTitle(title = "转移记录")

        initLayout()

        mBinding
            .listLayout
            .swipeRefresh
            .isRefreshing = true

        getData(pageNum)
    }

    private fun initLayout() {
        mBinding
            .listLayout
            .apply {
                emptyLayout
                    .also {
                        it.emptyHint.text = "暂无相关记录信息！"
                    }

                recycleList.also { layout ->
                    layout.loadLinear(
                        refresh = swipeRefresh,
                        onRefresh = { getData(0) },
                        onLoad = {
                            isLoadingMore
                                .conditionFalse {
                                    isLoadingMore = true
                                    getData(pageNum)
                                }
                        }
                    )

                    layout.addItemDecoration(SpaceTBDecoration())

                    mAdapter = SlimAdapter.creator()
                        .register<MasterTransferData>(R.layout.item_master_transfer_record) { jector, bean, _ ->
                            jector.apply {
                                text(R.id.item_record_out, bean.createDate.getTimeString())
                                text(R.id.item_record_in, bean.modifyDate.getTimeString())
                                text(R.id.item_record_memo, bean.memo.orEmpty())
                                text(R.id.item_record_from, bean.sender)
                                text(R.id.item_record_to, bean.receiver)
                                text(R.id.item_record_people, "${bean.quantity}个")
                                text(R.id.item_record_name, bean.operator)

                                visibility(
                                    R.id.item_record_in_ll,
                                    visibleValue { bean.status == "revoke" }
                                )

                                visibility(
                                    R.id.item_record_memo_ll,
                                    goneValue { bean.memo.isNullOrEmpty() }
                                )

                                visibility(
                                    R.id.item_record_back,
                                    visibleValue { bean.status == "transfer" }
                                )

                                visibility(
                                    R.id.item_record_arr,
                                    visibleValue { bean.status == "revoke" }
                                )

                                visibility(
                                    R.id.item_record_name,
                                    visibleValue { bean.status == "revoke" }
                                )

                                clicked(R.id.item_record_back) { transferBack(bean.id) }
                            }
                        }
                        .attachTo(layout)
                }
            }
    }

    /**
     * 会员转回按钮操作
     */
    private fun transferBack(itemId: String) {
        showHintDialog(
            title = "一键转回",
            hint = "确定要转回已转出的会员吗？",
            onSure = {
                getViewModel()
                    .request(
                        requestUrl = BaseUrl.mngTransBck,
                        params = arrayOf("id" to itemId),
                        onSuccess = {
                            showToast("会员转回成功！")

                            updateList()

                            // 更新员工列表
                            LiveEventBus
                                .get("masterList")
                                .post("refresh")

                            // 关闭员工详情页
                            ActivityStack
                                .screenManager
                                .popActivities(
                                    MasterInfoActivity::class.java
                                )
                        }
                    )
            }
        )
    }

    /**
     * 会员转移记录列表
     */
    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel()
            .transferList(
                page = index,
                onSuccess = {
                    mList.apply {
                        (index == 0).condition {
                            clear()
                            pageNum = 0
                        }

                        addItems(it)
                        it.notEmpty { pageNum++ }
                    }

                    mAdapter.setDataList(mList)
                },
                onFinally = {
                    isLoadingMore = false

                    mBinding
                        .listLayout
                        .apply {
                            swipeRefresh
                                .isRefreshing = false

                            emptyLayout
                                .emptyView
                                .isVisible = mList.isEmpty()
                        }
                }
            )
    }

    /**
     * 列表数据刷新
     */
    private fun updateList() {
        mBinding
            .listLayout
            .apply {
                swipeRefresh
                    .isRefreshing = true

                emptyLayout
                    .emptyView
                    .gone()
            }

        mList.notEmpty {
            clear()
            mAdapter.notifyDataSetChanged()
        }

        getData(0)
    }

}